home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / fips11.zip / SOURCE / FAT.CPP < prev    next >
C/C++ Source or Header  |  1994-05-25  |  4KB  |  143 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module fat.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/fips/source/main/RCS/fat.cpp 1.1 1994/05/25 22:19:46 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include "logdr_st.h"
  32. #include "global.h"
  33. #include "fat.h"
  34.  
  35. fat::fat (class logical_drive *logical_drive,int number)
  36. {
  37.     fat::logical_drive = logical_drive;
  38.     fat::number = number;
  39.     buffer = new sector;
  40.     start_sector = (number == 1) ? logical_drive->info().start_fat1 : logical_drive->info().start_fat2;
  41.     sector_in_buffer = -1;
  42. }
  43.  
  44. dword fat16::next_cluster (dword cluster_number)
  45. {
  46.     dword sector = cluster_number / 256;
  47.     int offset = (cluster_number % 256) * 2;
  48.  
  49.     if (sector != sector_in_buffer) read_sector (sector);
  50.  
  51.     return ((dword) buffer->data[offset] | ((dword) buffer->data[offset + 1] << 8));
  52. }
  53.  
  54. void fat::read_sector (dword sector)
  55. {
  56.     if (logical_drive->read_sector (sector + start_sector,buffer))
  57.         if (number == 1)
  58.             error ("Error reading FAT 1");
  59.         else
  60.             error ("Error reading FAT 2");
  61.     sector_in_buffer = sector;
  62. }
  63.  
  64. void fat16::check_against (class fat16 *fat2)
  65. {
  66.     printx ("Checking FAT ... ");
  67.  
  68.     for (int i=0;i<logical_drive->bpb().sectors_per_fat;i++)
  69.     {
  70.         read_sector (i);
  71.         fat2->read_sector (i);
  72.  
  73.         for (int j=0;j<512;j++) if (buffer->data[j] != fat2->buffer->data[j])
  74.             error ("FAT copies differ: FAT 1 -> %02Xh, FAT 2 -> %02Xh in sector %u, byte %u",buffer->data[j],fat2->buffer->data[j],i,j);
  75.  
  76.         if (i == 0)
  77.         {
  78.             if (buffer->data[0] != 0xf8)
  79.                 if (!global.override_media_descriptor)
  80.                     error ("Wrong Media Descriptor Byte in FAT: %02Xh",buffer->data[0]);
  81.             if ((buffer->data[1] != 0xff) || (buffer->data[2] != 0xff) || (buffer->data[3] != 0xff))
  82.                 warning ("Wrong FAT entries 1 & 2: %02X %02X %02X %02X",buffer->data[0],buffer->data[1],buffer->data[2],buffer->data[3]);
  83.         }
  84.     }
  85.     printx ("OK\n");
  86. }
  87.  
  88. void fat16::check_empty (dword new_start_sector)
  89. {
  90.     dword first_cluster = (new_start_sector - logical_drive->info().start_data) / logical_drive->bpb().sectors_per_cluster + 2;
  91.  
  92.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  93.  
  94.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  95.  
  96.     printx ("First Cluster: %lu\nLast Cluster: %lu\n\n",first_cluster,last_cluster);
  97.     printx ("Testing if empty ... ");
  98.  
  99.     for (dword i=first_cluster;i <= last_cluster;i++)
  100.     {
  101.         dword fat_entry = next_cluster (i);
  102.  
  103.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  104.         {
  105.             if (fat_entry == 0xffff)
  106.             {
  107.                 error ("New Partition not empty: cluster %lu ( FAT entry: <EOF> )",i);
  108.             }
  109.             else
  110.             {
  111.                 error ("New Partition not empty: cluster %lu ( FAT entry: %lu )",i,fat_entry);
  112.             }
  113.         }
  114.     }
  115.     printx ("OK\n");
  116. }
  117.  
  118. dword fat16::min_cluster (void)
  119. {
  120.     dword first_cluster = 2;
  121.  
  122.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  123.  
  124.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  125.  
  126.     printx ("Searching for free space ... ");
  127.  
  128.     dword i;
  129.  
  130.     for (i=last_cluster;i >= first_cluster;i--)
  131.     {
  132.         dword fat_entry = next_cluster (i);
  133.  
  134.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  135.         {
  136.             i++;
  137.             break;
  138.         }
  139.     }
  140.     printx ("OK\n\n");
  141.     return (i);
  142. }
  143.